package cn.itcast.mobilebrowser.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import cn.itcast.mobilebrowser.R;
import cn.itcast.mobilebrowser.adapter.BookMarksPagerAdapter;

public class BookMarksActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_marks);

        this.initView();
    }

    private void initView() {
        BookMarksPagerAdapter bookMarksPagerAdapter = new BookMarksPagerAdapter(this);

        ViewPager2 viewPager2 = findViewById(R.id.view_pager2);
        // 预加载前后一页
        viewPager2.setOffscreenPageLimit(1);
        viewPager2.setAdapter(bookMarksPagerAdapter);

        TabLayout tabs = findViewById(R.id.tabs);
        new TabLayoutMediator(tabs, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                switch (position) {
                    case 0:
                        tab.setText(R.string.tab_text_1);
                        break;
                    case 1:
                        tab.setText(R.string.tab_text_2);
                        break;
                }
            }
        }).attach();

        // 根据intent结果显示页面
        Intent intent = this.getIntent();
        int position = intent.getExtras().getInt("position");
        viewPager2.setCurrentItem(position);
    }

    public void onBackClick(View view) {
        // 返回键被按下
        finish();
    }
}